package com.ize.config;

import com.ize.Loginlnterceptor.JwtTokenAdminInterceptor;
import com.ize.Loginlnterceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册admin自定义拦截器...");
        //注册客户端端拦截器
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")//拦截这个路径的所有
                .excludePathPatterns("/admin/login","/admin/register")
                .excludePathPatterns("/doc.html", "/webjars/**", "/swagger-resources/**", "/v2/api-docs/**"); // 排除Swagger相关路径

        log.info("开始注册user自定义拦截器...");
        //注册用户端拦截器
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/student/**")//明确只拦截student相关路径
                .excludePathPatterns("/student/login0","/student/login1","/student/state")
                .excludePathPatterns("/doc.html", "/webjars/**", "/swagger-resources/**", "/v2/api-docs/**"); // 排除Swagger相关路径
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置资源映射........");
        // Knife4j和Swagger相关资源
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        // Swagger API文档
        registry.addResourceHandler("/swagger-resources/**")
                .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");
        registry.addResourceHandler("/v2/api-docs/**")
                .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");
    }

    /**
     * 扩展spring mvc框架的消息转换器  （后端返回给前端的数据进行统一的处理）---》日期类型的数据转换
     * @param converters
     */
//    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        //创建一个消息转换器对象
//        MappingJackson2HttpMessageConverter Converter = new MappingJackson2HttpMessageConverter();
//        //需要位消息转换器设置一个对象转换器，对象转换器可以为java对象序列化为json数据
//        Converter.setObjectMapper(new JacksonObjectMapper());
//        //将我们自己定义的消息转换器加入容器中,这里的 0 表示第一个使用我们的消息转换器
//        converters.add(0,Converter);
//
//    }
}
